<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Mapping the HTTP library into the Application &lt;sup&gt;2&lt;/sup&gt;&frasl;&lt;sub&gt;2&lt;/sub&gt; - Boost.Outcome documentation</title>
<link rel="stylesheet" href="../../../css/boost.css" type="text/css">
<meta name="generator" content="Hugo 0.52 with Boostdoc theme">
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>

<link rel="icon" href="../../../images/favicon.ico" type="image/ico"/>
<body><div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/advanced/interop/app-map-httplib1.html"><img src="../../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../../tutorial/advanced/interop.html"><img src="../../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/interop/app-map-filelib.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content">
  <div class="titlepage"><div><div><h1 style="clear: both">Mapping the HTTP library into the Application <code>2/2</code></h1></div></div></div>
  <p>If you remember the tutorial section on the <a href="../value-or-error"><code>value_or_error</code> Concept</a>,
this is an example of how to implement a custom <code>value_or_error</code> Concept converter
in Outcome:</p>

<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="c1">// Inject custom ValueOrError conversion
</span><span class="c1"></span><span class="n">BOOST_OUTCOME_V2_NAMESPACE_BEGIN</span>
<span class="k">namespace</span> <span class="n">convert</span>
<span class="p">{</span>
  <span class="c1">// Provide custom ValueOrError conversion from
</span><span class="c1"></span>  <span class="c1">// httplib::result&lt;U&gt; into any app::outcome&lt;T&gt;
</span><span class="c1"></span>  <span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span><span class="err"> </span><span class="nc">T</span><span class="p">,</span> <span class="k">class</span><span class="err"> </span><span class="nc">U</span><span class="o">&gt;</span>  <span class="c1">//
</span><span class="c1"></span>  <span class="k">struct</span> <span class="n">value_or_error</span><span class="o">&lt;</span><span class="n">app</span><span class="o">::</span><span class="n">outcome</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">,</span> <span class="n">httplib</span><span class="o">::</span><span class="n">result</span><span class="o">&lt;</span><span class="n">U</span><span class="o">&gt;&gt;</span>
  <span class="p">{</span>
    <span class="c1">// False to indicate that this converter wants `result`/`outcome`
</span><span class="c1"></span>    <span class="c1">// to NOT reject all other `result`
</span><span class="c1"></span>    <span class="k">static</span> <span class="k">constexpr</span> <span class="kt">bool</span> <span class="n">enable_result_inputs</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span>
    <span class="c1">// False to indicate that this converter wants `outcome` to NOT
</span><span class="c1"></span>    <span class="c1">// reject all other `outcome`
</span><span class="c1"></span>    <span class="k">static</span> <span class="k">constexpr</span> <span class="kt">bool</span> <span class="n">enable_outcome_inputs</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span>

    <span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span><span class="err"> </span><span class="nc">X</span><span class="p">,</span>                                                                              <span class="c1">//
</span><span class="c1"></span>              <span class="k">typename</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">enable_if_t</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">is_same</span><span class="o">&lt;</span><span class="n">httplib</span><span class="o">::</span><span class="n">result</span><span class="o">&lt;</span><span class="n">U</span><span class="o">&gt;</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">decay_t</span><span class="o">&lt;</span><span class="n">X</span><span class="o">&gt;&gt;::</span><span class="n">value</span>  <span class="c1">//
</span><span class="c1"></span>                                          <span class="o">&amp;&amp;</span> <span class="n">std</span><span class="o">::</span><span class="n">is_constructible</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span> <span class="n">U</span><span class="o">&gt;::</span><span class="n">value</span><span class="o">&gt;&gt;</span>                   <span class="c1">//
</span><span class="c1"></span>    <span class="k">constexpr</span> <span class="n">app</span><span class="o">::</span><span class="n">outcome</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="k">operator</span><span class="p">()(</span><span class="n">X</span> <span class="o">&amp;&amp;</span><span class="n">src</span><span class="p">)</span>
    <span class="p">{</span>
      <span class="c1">// Forward any successful value, else synthesise an exception ptr
</span><span class="c1"></span>      <span class="k">return</span> <span class="n">src</span><span class="p">.</span><span class="n">has_value</span><span class="p">()</span> <span class="o">?</span>                              <span class="c1">//
</span><span class="c1"></span>             <span class="n">app</span><span class="o">::</span><span class="n">outcome</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">{</span><span class="n">std</span><span class="o">::</span><span class="n">forward</span><span class="o">&lt;</span><span class="n">X</span><span class="o">&gt;</span><span class="p">(</span><span class="n">src</span><span class="p">).</span><span class="n">value</span><span class="p">()}</span>  <span class="c1">//
</span><span class="c1"></span>             <span class="o">:</span>
             <span class="n">app</span><span class="o">::</span><span class="n">outcome</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">{</span><span class="n">app</span><span class="o">::</span><span class="n">make_httplib_exception</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">forward</span><span class="o">&lt;</span><span class="n">X</span><span class="o">&gt;</span><span class="p">(</span><span class="n">src</span><span class="p">))};</span>
    <span class="p">}</span>
  <span class="p">};</span>
<span class="p">}</span>  <span class="c1">// namespace convert
</span><span class="c1"></span><span class="n">BOOST_OUTCOME_V2_NAMESPACE_END</span>
</code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/finale.cpp#L257" class="code-snippet-url" target="_blank">View this code on Github</a></div>


<p>The first thing that you should note is that these custom converters must be injected
directly into the <code>BOOST_OUTCOME_V2_NAMESPACE::convert</code> namespace, and they must partially
or completely specialise <a href="../../../reference/converters/value_or_error.html" class="api-reference"><code>value_or_error&lt;T, U&gt;</code></a>
. Here we specialise the
converter for <code>value_or_error</code> conversions from <code>httplib::result&lt;U&gt;</code> to <code>app::outcome&lt;T&gt;</code>
i.e. from our third party HTTP library&rsquo;s error type into our application&rsquo;s <code>outcome</code>
type (which is unique to our application, as we hard code an <code>app</code>-local error type).</p>

<p>The second thing to note is that you need to set <code>enable_result_inputs</code> and <code>enable_outcome_inputs</code>
appropriately, otherwise <code>result</code> and <code>outcome</code> inputs will not be matched by this
converter<sup class="footnote-ref" id="fnref:1"><a href="#fn:1">1</a></sup>. In this converter, we really do wish to convert other <code>result</code> and
<code>outcome</code> inputs, so we mark these booleans as <code>true</code>.</p>

<p>The third thing to note is the requirements on <code>operator()</code>. If the requirements are
not met, the <code>value_or_error</code> converting constructor in <code>basic_result</code> and <code>basic_outcome</code>
disables. Note the requirement that the decayed <code>operator()</code> input <code>X</code> matches
<code>httplib::result&lt;U&gt;</code>, and that <code>T</code> is constructible from <code>U</code>. This means that the
<a href="../../../reference/types/basic_result/explicit_valueorerror_converting_constructor.html" class="api-reference"><code>explicit basic_result(concepts::value_or_error&lt;T, E&gt; &amp;&amp;)</code></a>
 and <a href="../../../reference/types/basic_outcome/explicit_valueorerror_converting_constructor.html" class="api-reference"><code>explicit basic_outcome(concepts::value_or_error&lt;T, E&gt; &amp;&amp;)</code></a>

constructors are available if, and only if, the input type is a <code>httplib::result&lt;U&gt;</code>,
and the result&rsquo;s value type is constructible from the input&rsquo;s value type.</p>

<p>If <code>operator()</code> is available, it naturally converts a <code>httplib::result&lt;U&gt;</code> into an
<code>app::outcome&lt;T&gt;</code> by either forwarding any success as-is, or calling <code>app::make_httplib_exception()</code>
to type erase the <code>httplib::failure</code> into an <code>app::httplib_error</code>.</p>
<div class="footnotes">

<hr />

<ol>
<li id="fn:1">Here we refer to <code>result</code> and <code>outcome</code> as defined by this specific Outcome library. If <code>result</code> or <code>outcome</code> from another Outcome implementation is seen, those <strong>always</strong> must get parsed via the <code>ValueOrError</code> matching conversion framework.
 <a class="footnote-return" href="#fnref:1"><sup>[return]</sup></a></li>
</ol>
</div>


        </div><p><small>Last revised: December 15, 2020 at 12:22:39 UTC</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/advanced/interop/app-map-httplib1.html"><img src="../../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../../tutorial/advanced/interop.html"><img src="../../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/interop/app-map-filelib.html"><img src="../../../images/next.png" alt="Next"></a></div></body>
</html>
